package com.xin.constants;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

public class CommonResult<T> {

    private Integer code;


    private String  msg;


    private T data;

    private Long page;

    private Long size;

    private Long total;



    public static <T> CommonResult<T> success(T data,String msg){

        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(200);
        commonResult.setData(data);
        commonResult.setMsg(msg);
        //包含了分页信息
        if (data instanceof Page){
            Page page = (Page) data;
            commonResult.setTotal(page.getTotal());
            commonResult.setPage(page.getPages());
            commonResult.setSize(page.getPages());
            List records = page.getRecords();
            commonResult.setData((T) records);

        }
        return commonResult;
    }

    public static <T> CommonResult<T> error(Integer code,String msg){
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setMsg(msg);
        commonResult.setCode(code);
        commonResult.setData(null);
        return commonResult;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


    public Long getPage() {
        return page;
    }

    public void setPage(Long page) {
        this.page = page;
    }

    public Long getSize() {
        return size;
    }

    public void setSize(Long size) {
        this.size = size;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }
}
